package site.user.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import modules.recruit.entity.Region;
import modules.recruit.entity.RegionImage;
import modules.recruit.service.RegionImageService;
import modules.recruit.service.RegionService;
import modules.system.entity.Attach;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import site.user.form.RegionImageFormSite;

import com.guinea.core.service.EntityService;
import com.guinea.core.utils.StringUtils;
import com.guinea.core.web.StrutsAjaxAction;

import ex.system.service.AttachServiceEx;

public class RegionImageActionSite extends StrutsAjaxAction {
	private RegionImageService regionImageService;

	private RegionService regionService;

	private Region region;

	private Long regionId;

	private String errorMessages;

	AttachServiceEx attachServiceEx;

	public EntityService getEntityService() {
		return regionImageService;
	}

	public AttachServiceEx getAttachServiceEx() {
		return attachServiceEx;
	}

	public void setAttachServiceEx(AttachServiceEx attachServiceEx) {
		this.attachServiceEx = attachServiceEx;
	}

	public String entity2json(Object object) {
		RegionImage regionImage = (RegionImage) object;
		Attach attach = null;
		if (regionImage != null && regionImage.getAttachId() != null)
			attach = getAttachServiceEx().getAttach(regionImage.getAttachId());
		String attachPath = "";
		String attachId = "";
		if (attach != null) {
			attachId = attach.getId().toString();
			attachPath = StringUtils.defaultString(attach.getWebPath());
		}
		String result = "";
		result += "{";
		result += "\"id\" : \""
				+ (regionImage.getId() == null ? "" : regionImage.getId()
						.longValue()
						+ "") + "\"";
		result += ", \"regionId\" : \""
				+ (regionImage.getRegion() == null ? "" : regionImage
						.getRegion().getId().longValue()
						+ "") + "\"";
		result += ", \"typeCode\" : \""
				+ StringUtils.defaultString(regionImage.getTypeCode()) + "\"";
		result += ", \"attachId\" : \"" + attachId + "\"";
		result += ", \"attachPath\" : \"" + attachPath + "\"";
		result += "}";
		return result;
	}

	protected String saveWrapper(String text) {
		return "<script>window.parent.successRegionImage('" + text
				+ "')</script>";
	}

	public RegionImageService getRegionImageService() {
		return regionImageService;
	}

	public void setRegionImageService(RegionImageService regionImageService) {
		this.regionImageService = regionImageService;
	}

	public RegionService getRegionService() {
		return regionService;
	}

	public void setRegionService(RegionService regionService) {
		this.regionService = regionService;
	}

	/**
	 * 初始化业务对象的函数.
	 */
	protected void findEntity(ActionForm form, HttpServletRequest request) {
		super.findEntity(form, request);
		regionId = (Long) getEntityId(request, "regionId", Long.class);
		if (regionId != null) {
			region = getRegionService().getRegion(regionId);
		}
	}

	protected void initProperties() {
		super.initProperties();
		regionId = null;
		region = null;
		errorMessages = "";
	}

	protected boolean validateRegion(HttpServletRequest request) {
		if (region == null) {
			return valid = false;
		}
		return true;
	}

	public ActionForward ajaxListValidator(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		initFirst(form, request);
		if (!validateRegion(request))
			return handleRegionMissing(mapping, request, response);
		return null;
	}

	/**
	 * 取得SkoruShareholder对象列表
	 */
	public ActionForward ajaxList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		List list = getRegionImageService().getByRegion(regionId);
		String result = collection2json(list);
		renderText(response, "{\"data\":" + result + "}");
		return null;
	}

	public ActionForward ajaxSaveValidator(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		initFirst(form, request);
		if (!validateRegion(request))
			return handleRegionMissing(mapping, request, response);
		if (!validateFile(form))
			return handleFile(mapping, request, response);
		return null;
	}
	
	protected boolean validateFile(ActionForm form) {
		RegionImageFormSite imageForm = (RegionImageFormSite) form;
		if (imageForm == null) {
			errorMessages += "提交信息为空。";
			return valid = false;
		}
		FormFile f = imageForm.getFile();
		if (f == null || f.getFileSize() < 1
				|| f.getFileSize() > 2 * 1024 * 1024) {
			errorMessages += "请上传一个大小不超过2M的图片，格式为JPG,GIF或PNG格式。";
			return valid = false;
		}
		return true;
	}

	/**
	 * 保存对象的Action函数.
	 */
	public ActionForward ajaxSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		doInitEntity(request, form);

		doSaveEntity(request, form);
		
		renderHtml(response,
				saveWrapper("{\"message\":\"保存成功。\",\"data\":"+entity2json(entity)+"}"));
		return null;
	}

	protected ActionForward handleFile(ActionMapping mapping,
			HttpServletRequest request, HttpServletResponse response) {
		renderText(response, "{\"error\":\"" + errorMessages + "\"}");
		return null;
	}

	protected void doInitEntity(HttpServletRequest request, ActionForm form) {
		super.doInitEntity(request ,form);
		RegionImageFormSite imageForm = (RegionImageFormSite) form;
		Long attachId = getAttachServiceEx().saveFile(imageForm.getFile());
		if (isCreateOperation) {
			RegionImage regionImage = (RegionImage) entity;
			regionImage.setAttachId(attachId);
			regionImage.setRegion(region);
			entity = regionImage;
		}
	}

	protected ActionForward handleRegionMissing(ActionMapping mapping,
			HttpServletRequest request, HttpServletResponse response) {
		renderText(response, "{\"error\":\"找不到计划场地记录!\"}");
		return null;
	}

}
